<?php 
/**
 * 景点相关的控制器类
 */
class ScenicAction extends ManagerAction{
	/**
	 * 景点列表
	 */
	public function clist(){
		$Scenic = D('Scenic');
		$count=$Scenic->where($where)->count();
		$Page = new Page($count,C('PAGE_COUNT'));
		$limit = "{$Page->firstRow},{$Page->listRows}";
		$list = $Scenic->limit($limit)->select();
		$this->assign('list',$list);
		$this->assign('page',$Page->show());
		$this->display();
	}
	/**
	 * 添加景点
	 */
	public function add(){
		if($_POST){
			$_POST['thumb']			= $_POST['images'][0];
			$_POST['picture_urls']	= json_encode($_POST['images']);
			$_POST['uid']			= Session::get('uid');
			$Scenic = D('Scenic');
			if($Scenic->create()){
				$Scenic->add();
				$this->success('添加成功');
			}else{
				$this->error($Scenic->geterror());
			}
		}else{
			$this->display();
		}
	}
	/**
	 * 修改景点
	 */
	public function update(){
		$id=$_REQUEST['id'];
		$Scenic = D('Scenic');
		if($_POST){
			$scenic_data['id']		= $_POST['id'];
			$scenic_data['name']		= $_POST['name'];
			$scenic_data['address'] 	= $_POST['address'];
			$scenic_data['content'] 	= $_POST['content'];
			$scenic_data['local_stay'] 	= $_POST['local_stay'];
			$scenic_data['local_diet'] 	= $_POST['local_diet'];
			$scenic_data['travel_time'] = $_POST['travel_time'];
			$scenic_data['picture_urls']= json_encode($_POST['images']);
			$Scenic->data($scenic_data)->save();
			$this->success("修改成功");
		}else{
			$scenic_info = $Scenic->where("id={$id}")->find();
			$images_list = json_decode($scenic_info['picture_urls']);
			$this->assign('scenic_info',$scenic_info);
			$this->assign('image_list',$images_list);
			$this->display();
		}
	}
	/**
	 * 删除景点
	 */
	public function del(){
		$id=intval($_GET['id']);
		$image_list = M('scenic_image')->where("sid={$id}")->field('image_url')->select();
		foreach($image_list as $v){
			$filename =  $_SERVER["DOCUMENT_ROOT"].'/Public/uploads/'.$v['image_url'];
			@unlink($filename);
		}
		M('scenic_image')->where("sid={$id}")->delete();
		M('scenic')->where("id={$id}")->delete();
		$this->success("删除成功");
	}
	/**
	 * 获取指定id的图片列表
	 */
	public function getImageList(){
		
	}
	public function delImage(){
		$id=intval($_GET['id']);		
		$file = M('scenic_image')->where("id={$id}")->getfield("image_url");
		M('scenic_image')->where("id={$id}")->delete();
		$filename =  $_SERVER["DOCUMENT_ROOT"].'/Public/uploads/'.$file;
		$data['flag'] = 0;
		if( is_file( $filename ) ){
			if( unlink($filename) )	{
				$data['msg'] = '文件删除成功';
				$data['flag'] = 1;
			}else	{
				$data['msg'] = '文件删除失败，权限不够';
			}
		}else		{
			$data['msg'] = '不是有一个有效的文件';
		}
		$this->ajaxReturn($data);
		
	}
}
?>
